package listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import service.SignBroadSender;
import ui.MainUI;
import ui.QuestionNumberUI;
import ui.ShowAnswerUI;
import ui.ShowSignUI;
import util.JAVAFileFilter;
import util.Java_JDBC;
import config.Config;
import config.MainConfig;
import config.SocketConfig;

public class MainListener implements ActionListener{

	private JButton startSignButton;
	private JButton endSignButton;
	private JButton startAnswer;
	private JButton endAnswer;
	private JButton showSign;
	private JButton showAnswer;
	private JButton clearSignedButton;
	private JButton clearAnswerButton;
	private JButton paperButton;
	private MainUI mainUI;
	
	public MainListener(MainUI mainUI) {
		this.startSignButton = mainUI.startSignButton;
		this.endSignButton = mainUI.endSignButton;
		this.startAnswer = mainUI.startAnswer;
		this.endAnswer = mainUI.endAnswer;
		this.showSign = mainUI.showSign;
		this.showAnswer = mainUI.showAnswer;
		this.clearSignedButton = mainUI.clearSignedButton;
		this.clearAnswerButton = mainUI.clearAnswerButton;
		this.paperButton = mainUI.paperButton;
		this.mainUI = mainUI;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == startSignButton) {
			if(Config.isSign) {
				JOptionPane.showMessageDialog(mainUI, "错误：已经开启了签到"); 
				return;
			}
			JOptionPane.showMessageDialog(mainUI, "注意：开始签到"); 
			Config.isSign = true;
			String sendMsg = "startSign:"+Config.className+":"+SocketConfig.ipAddress+":"+SocketConfig.socketPort;
			Thread thread = new Thread(new SignBroadSender(sendMsg, SocketConfig.signBroadPort));
			thread.start();
		}else if(e.getSource() == endSignButton) {
			if(!Config.isSign) {
				JOptionPane.showMessageDialog(mainUI, "警告：还未开始签到"); 
				return;
			}
			JOptionPane.showMessageDialog(mainUI, "注意：结束签到"); 
			Config.isSign = false;
		}else if(e.getSource() == startAnswer) {
			try {
				if(!Config.isEndAnswer&&Config.isAnswer) {
					JOptionPane.showMessageDialog(mainUI, "错误：请先结束上一次答题"); 
					return;
				}
				Config.isAnswer = false;
				Config.isEndAnswer = false;
				new QuestionNumberUI().setVisible(true);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}else if(e.getSource() == endAnswer) {
			if(!Config.isAnswer) {
				JOptionPane.showMessageDialog(mainUI, "警告：还未开始答题"); 
				return;
			}
			JOptionPane.showMessageDialog(mainUI, "注意：结束答题"); 
			Config.isEndAnswer = true;
			Config.isAnswer = false;
		}else if(e.getSource() == showSign) {
			new ShowSignUI().setVisible(true);
		}else if(e.getSource() == showAnswer) {
			try {
				new ShowAnswerUI().setVisible(true);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}else if(e.getSource() == this.clearSignedButton) {
			JOptionPane.showMessageDialog(mainUI, "警告：清除了签到表"); 
			try {
				int signClearState = new Java_JDBC().clearSignedTable();
				if(signClearState == 0) {
					Config.signNumber=0;
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}else if(e.getSource() == this.clearAnswerButton) {
			JOptionPane.showMessageDialog(mainUI, "警告：清除了答题表"); 
			try {
				new Java_JDBC().clearAnswerTable();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}else if(e.getSource() == paperButton) {
			JFileChooser fileChooser = new JFileChooser("C:\\winnt");// 以c:\\winnt为打开文件为对话框的默认路径
			// 利用addChoosableFileFilter()方法加入欲过滤的文件类型,使用addChoosableFileFilter()可以加入多种文件类型,
			// 若你只需要过滤出一种文件类型,可使用setFileFilter()方法.
			fileChooser.addChoosableFileFilter(new JAVAFileFilter("txt"));
			fileChooser.addChoosableFileFilter(new JAVAFileFilter("docx"));
			fileChooser.addChoosableFileFilter(new JAVAFileFilter("doc"));
			fileChooser.addChoosableFileFilter(new JAVAFileFilter("xls"));
			fileChooser.addChoosableFileFilter(new JAVAFileFilter("xlsx"));
			fileChooser.addChoosableFileFilter(new JAVAFileFilter("ppt"));
			fileChooser.addChoosableFileFilter(new JAVAFileFilter("pptx"));
			
			int result = fileChooser.showOpenDialog(mainUI);
			if (result == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
				String paperPath = file.getAbsolutePath();
				String paperName = file.getName();
				try {
					new Java_JDBC().addPaper(paperName, paperPath);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				//TODO 将文件名称和文件路径写入到数据库当中
				System.out.println("paperPath: "+paperPath+"    paperName:"+paperName);
			} else if (result == fileChooser.CANCEL_OPTION) {
				JOptionPane.showMessageDialog(mainUI, "没有选择上传文件");  
			}
		}
		else if(e.getActionCommand().equals("Bird")) {
			System.out.println("Bird");
			MainUI.image = new ImageIcon(MainConfig.birdBackPath).getImage();
			mainUI.repaint();
		}else if(e.getActionCommand().equals("Cat")) {
			System.out.println("Cat");
			MainUI.image = new ImageIcon(MainConfig.catBackPath).getImage();
			mainUI.repaint();
		}else if(e.getActionCommand().equals("Dog")) {
			System.out.println("Dog");
			MainUI.image = new ImageIcon(MainConfig.dogBackPath).getImage();
			mainUI.repaint();
		}else if(e.getActionCommand().equals("Rabbit")) {
			System.out.println("Rabbit");
			MainUI.image = new ImageIcon(MainConfig.rabbitBackPath).getImage();
			mainUI.repaint();
		}else if(e.getActionCommand().equals("Pig")) {
			System.out.println("Pig");
			MainUI.image = new ImageIcon(MainConfig.pigBackPath).getImage();
			mainUI.repaint();
		}
	}
}
